//
//  MsgDetialViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/1.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "MsgDetialViewController.h"
#import "MsgDetailModel.h"
#import "MsgDetialTableViewCell.h"
#import "MsgHeadTableViewCell.h"
#import "LinHeadView.h"
#import "MsgReceiveModel.h"
@interface MsgDetialViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *mainTableView;

@property(nonatomic,strong)MsgRecriveData *DataModel;

@end

@implementation MsgDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.mainTableView];
    
    [self loadDatas];
    
    // Do any additional setup after loading the view.
}


-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/messageDetail.json" andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
 
        if (responseStatus==1) {
            //请求成功
            self.DataModel=[MsgRecriveData mj_objectWithKeyValues:responseObject[@"data"]];
            
            _block();
            
            [self.mainTableView reloadData];
            
        }else if (responseStatus==0||responseStatus==2)
        {
  
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
    
    
}


-(TB_TableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _mainTableView.delegate=self;
        _mainTableView.dataSource=self;
 
        _mainTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        _mainTableView.estimatedRowHeight=50;
        _mainTableView.estimatedSectionHeaderHeight=0;
        _mainTableView.estimatedSectionFooterHeight=0;
        
        _mainTableView.tableFooterView=[[UIView alloc]init];
        
        [_mainTableView registerNib:[UINib nibWithNibName:@"MsgHeadTableViewCell" bundle:nil] forCellReuseIdentifier:@"Tcell"];
        
        [_mainTableView registerNib:[UINib nibWithNibName:@"MsgDetialTableViewCell" bundle:nil] forCellReuseIdentifier:@"Ccell"];
        
        [_mainTableView registerClass:[LinHeadView class] forHeaderFooterViewReuseIdentifier:@"head"];
        
    }
    return _mainTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section==0) {
        
        return 3;
    }
    
    return 1;
    
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        
        MsgHeadTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Tcell" forIndexPath:indexPath];
        
        switch (indexPath.row) {
            case 0:
            {
                cell.tipLabel.text=self.DataModel.title;
                
                cell.tipLabel.font=[UIFont boldSystemFontOfSize:19];
                
                cell.tipLabel.textColor=[UIColor blackColor];
                
                
            }
                break;
            case 1:
            {
                cell.tipLabel.text=self.DataModel.senderName;
                
                cell.tipLabel.font=[UIFont systemFontOfSize:17];
                
                cell.tipLabel.textColor=[UIColor blackColor];
                
            }
                break;
            case 2:
            {
                cell.tipLabel.text=self.DataModel.sendTime;
                
                cell.tipLabel.font=[UIFont systemFontOfSize:13];
                
                cell.tipLabel.textColor=[UIColor darkGrayColor];
                
            }
                break;
                
            default:
                break;
        }
        
        return cell;
        
    }else{
        
        MsgDetialTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Ccell" forIndexPath:indexPath];
        
        cell.TipLabel.text=self.DataModel.content;
        
        cell.TipLabel.font=[UIFont systemFontOfSize:15];
        
        cell.TipLabel.textColor=[UIColor darkGrayColor];
        
        return cell;
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 3;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    LinHeadView *headView=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    
    return headView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
